package gov.kqqzyyy.monkey.orderpad.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import gov.kqqzyyy.monkey.orderpad.R;

public class WebService {
    private String METHOD_NAME = null;
    private String PARM = null;

    private ByteArrayOutputStream outStream;
    private InputStream inputStream;
    private byte[] data;
    private String mServerUrl;

    public WebService(Context context) {
        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        mServerUrl = "http://" + mSharedPreferences.getString("serverAddress", context.getResources()
                .getString(R.string.default_server)) + "/api/API/";
    }

    public void setPARM(String PARM) {
        this.PARM = PARM;
    }

    public void setMETHOD_NAME(String METHOD_NAME) {
        this.METHOD_NAME = METHOD_NAME;
    }

    //调用C#写的接口
    public String getUrlServerData() {
        HttpURLConnection connection = null;
        String path, result = null;
        if (PARM != null) {
            path = mServerUrl + METHOD_NAME + "?" + PARM;
        } else {
            path = mServerUrl + METHOD_NAME;
        }
        try {
            if (inputStream != null) {
                inputStream.close();
                inputStream = null;
            }
            if (outStream != null) {
                outStream.flush();
                outStream.close();
                outStream = null;
            }
            if (data != null) {
                data = null;
            }
            URL url = new URL(path);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5000); //设置连接超时为15秒
            connection.setRequestMethod("GET"); //设定请求方式
            connection.connect(); //建立到远程对象的实际连接
            int code = connection.getResponseCode();
            if (code != HttpURLConnection.HTTP_OK) {//判断是否正常响应数据
                return null;
            } else {
                inputStream = connection.getInputStream(); //返回打开连接读取的输入流
                outStream = new ByteArrayOutputStream();
                data = new byte[1024];
                int count;
                while ((count = inputStream.read(data, 0, 1024)) != -1) {
                    outStream.write(data, 0, count);
                }
                result = new String(outStream.toByteArray(), StandardCharsets.UTF_8);
                outStream.flush();
                outStream.close();
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("APE", "WebService E: " + e.getMessage());
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return result;
    }

    public void setmServerUrl(String mServerUrl) {
        this.mServerUrl = mServerUrl;
    }

    //    //调用java写的WebService
//    public String getServerData() {
//        String result = null;
//        END_POINT = "http://" + mContext.getResources().getString(R.string.default_webservice) + "/axis2/services/HZP";
//        String soapAction = NAME_SPACE + "/" + METHOD_NAME;
//        SoapObject soapObject = new SoapObject(NAME_SPACE, METHOD_NAME);
//        if (PARMS != null && PARMS.length > 0) {
//            for (int i = 0; i < PARMS.length; i++) {
//                soapObject.addProperty("arg" + i, PARMS[i]);
//            }
//        }
//        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//        envelope.bodyOut = soapObject;
//        HttpTransportSE transportSE = new HttpTransportSE(END_POINT);
//        try {
//            transportSE.call(soapAction, envelope);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        SoapObject resultObject = (SoapObject) envelope.bodyIn;
//        if (resultObject != null) {
//            if (resultObject.getProperty(0) != null) {
//                result = resultObject.getProperty(0).toString();
//            }
//        }
//        return result;
//    }
}
